package telas;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

import controles.IpText;

public class Cliente {

	private Shell sShell = null;  //  @jve:decl-index=0:visual-constraint="10,10"
	private TabFolder clienteTabFolder = null;
	private Composite clienteComposite = null;
	private IpText ipText = null;  //  @jve:decl-index=0:visual-constraint="553,160"
	private Composite menuComposite = null;
	private Label ipLabel = null;
	private Label nomeLabel = null;
	private Text nomeText = null;
	private Button conectarButton = null;
	private Button fecharButton = null;
	private Label portaLabel = null;
	private Text portaText = null;
	private boolean okPressed = false;
	
	private String ip = null;
	private int porta;
	private String nick = null;

	public void open()
	{
		createSShell();
		centerScreen(sShell);
		sShell.setVisible(true);
		ipText.setFocus();
		
		Display display = Display.getCurrent();

		while (!sShell.isDisposed())
		{
			if (!display.readAndDispatch())
				display.sleep();
		}
		sShell.dispose();
	}
	private void createSShell() 
	{
		sShell = new Shell(SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
		sShell.setText("DamasRede v1.0");
		sShell.setText("Damas v1.0");
		createClienteTabFolder();
		sShell.setSize(new Point(314, 280));
		sShell.setLayout(null);
	}

	private void createClienteTabFolder() 
	{
		clienteTabFolder = new TabFolder(sShell, SWT.NONE);
		createClienteComposite();
		clienteTabFolder.setBounds(new Rectangle(5, 5, 295, 238));
		TabItem tabItem = new TabItem(clienteTabFolder, SWT.NONE);
		tabItem.setText("Cliente");
		tabItem.setControl(clienteComposite);
	}
	private void centerScreen(Shell shell)
	{
	    int x = Display.getCurrent().getPrimaryMonitor().getClientArea().x + (Display.getCurrent().getPrimaryMonitor().getClientArea().width - shell.getBounds().width) / 2;
	    int y = Display.getCurrent().getPrimaryMonitor().getClientArea().y + (Display.getCurrent().getPrimaryMonitor().getClientArea().height - shell.getBounds().height) / 2;
	    shell.setLocation(x, y);
	}
	private void createClienteComposite() 
	{
		clienteComposite = new Composite(clienteTabFolder, SWT.NONE);
		clienteComposite.setLayout(null);
		
		ipText = new IpText(clienteComposite,SWT.BORDER);
		ipText.setLocation(new Point(15, 35));
		ipText.setSize(new Point(160, 22));
		portaText = new Text(clienteComposite, SWT.BORDER);
		portaText.setBounds(new Rectangle(15, 87, 76, 19));
		portaText.addKeyListener(new org.eclipse.swt.events.KeyAdapter() {
			public void keyPressed(org.eclipse.swt.events.KeyEvent e) 
			{
				if(!Character.isDigit(e.keyCode))
				{
					if(e.keyCode == 8)
						return;
					e.doit = false;
				}
			}
		});
		nomeText = new Text(clienteComposite, SWT.BORDER);
		nomeText.setSize(new Point(176, 19));
		nomeText.setLocation(new Point(15, 138));
		nomeText.addKeyListener(new org.eclipse.swt.events.KeyAdapter() {
			public void keyPressed(org.eclipse.swt.events.KeyEvent e) {
				if(e.keyCode==13)
					conectar();
			}
		});
		createMenuComposite();
		ipLabel = new Label(clienteComposite, SWT.NONE);
		ipLabel.setText("IP do Servidor");
		ipLabel.setLocation(new Point(15, 15));
		ipLabel.setSize(new Point(77, 13));
		nomeLabel = new Label(clienteComposite, SWT.NONE);
		nomeLabel.setText("Nick");
		nomeLabel.setLocation(new Point(15, 118));
		nomeLabel.setSize(new Point(93, 13));
		portaLabel = new Label(clienteComposite, SWT.NONE);
		portaLabel.setBounds(new Rectangle(14, 68, 80, 13));
		portaLabel.setText("Porta");
	}
	private void conectar()
	{
		if(ipText.getText().compareTo("")==0)
			return;
		if(nomeText.getText().compareTo("")==0)
			return;
		if(portaText.getText().compareTo("")==0)
			return;
		
		ip = ipText.getText();
		nick = nomeText.getText();
		porta = Integer.parseInt(portaText.getText());
		okPressed = true;
		sShell.close();
	}
	private void createMenuComposite() {
		menuComposite = new Composite(clienteComposite, SWT.BORDER);
		menuComposite.setLayout(null);
		menuComposite.setBackground(new Color(Display.getCurrent(), 232, 232, 232));
		menuComposite.setLocation(new Point(15, 169));
		menuComposite.setSize(new Point(260, 35));
		conectarButton = new Button(menuComposite, SWT.NONE);
		conectarButton.setText("Conectar");
		conectarButton.setLocation(new Point(5, 5));
		conectarButton.setSize(new Point(120, 23));
		conectarButton
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) 
					{
						conectar();
					}
				});
		fecharButton = new Button(menuComposite, SWT.NONE);
		fecharButton.setText("Fechar");
		fecharButton.setSize(new Point(120, 23));
		fecharButton.setLocation(new Point(132, 5));
		fecharButton
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
						sShell.close();
					}
				});
	}
	public String getIp() {
		return ip;
	}
	public String getNome() {
		return nick;
	}
	public int getPort() {
		return porta;
	}
	public boolean isOkPressed() {
		return okPressed;
	}

}
